/**
 * Created by Administrator on 2017/12/19.
 */
import cookie from "js-cookie";
import { Toast } from 'antd-mobile';
import { routerRedux } from 'dva/router';
import { orgchildren,organization } from '../../services/organization';

export default {

  namespace: 'organizationss',

  state: {
    orgLists:[],
    orgs:[],
    orgBottom:[],
  },
  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(({pathname, query}) => {
        if(pathname === '/work/zzadminister'||pathname === '/work/democraticappraisal'||pathname === '/work/dyadminister'||pathname === '/work/dfadminister'||pathname === '/work/hdadminister') {
          dispatch({
            type:'fetch',
          })
        }
      })
    }
  },
  effects: {
    //获取组织下属组织
    *fetch({payload}, { call, put, select }) {
      let newsObject = sessionStorage.getItem('userdata');
      newsObject = JSON.parse(newsObject);
      if(!newsObject){
        //跳转到登陆页
        yield put(routerRedux.push('/login'));
      }
      const {data} =yield call(orgchildren,{code:newsObject[1].orgs[0][0].org_code});
      if (data.code == 200) {
        yield put({
          type: 'success',
          payload: {
            orgLists: data.ret
          }
        });
      }
    else {
        Toast.success('查询失败！', 1);
      }
    },

    //获取党支部
    *orgBottom({payload}, { call, put, select }) {
      console.log("payload",payload);
      const data = yield call(organization, {code:payload.code?payload.code:"",type_code: '631'});
      if (data.data.code == 200) {
        yield put({
          type: 'success',
          payload: {
            orgBottom: data.data.ret
          }
        });
      }
      else {
        Toast.success('查询失败！', 1);
      }
    },
    //获取党总支
    *org({payload}, { call, put, select }) {
      const data = yield call(organization, {type_code: '621'});
      if (data.data.code == 200) {
        yield put({
          type: 'success',
          payload: {
            orgs: data.data.ret
          }
        });
      }
      else {
        Toast.success('查询失败！', 1);
      }
    },
  },

  reducers: {
    success(state, action){
      return {...state, ...action.payload};
    }
  }
};
